package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmFactoryOrderByField;
import com.zb.entity.ZjAdmOrderByField;
import com.zb.entity.dto.FactoryOrderByFieldDto;
import com.zb.entity.dto.OrderByFieldDto;
import com.zb.entity.vo.FactoryOrderByFieldListVo;
import com.zb.entity.vo.OrderByFieldListVo;
import com.zb.mapper.ZjAdmFactoryOrderByFieldMapper;
import com.zb.service.ZjAdmFactoryOrderByFieldService;
import com.zb.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台厂商工单列表排序字段表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-04-10
 */
@Service
public class ZjAdmFactoryOrderByFieldServiceImpl extends ServiceImpl<ZjAdmFactoryOrderByFieldMapper, ZjAdmFactoryOrderByField> implements ZjAdmFactoryOrderByFieldService {
    @Resource
    private ZjAdmFactoryOrderByFieldMapper admFactoryOrderByFieldMapper;

    // 新增或修改工单列表排序字段
    @Override
    public void save(FactoryOrderByFieldDto fieldDto) {
        Assert.isNull(admFactoryOrderByFieldMapper.existByType(fieldDto.getId(), fieldDto.getType()), "当前排序字段类型已重复");
        ZjAdmFactoryOrderByField field = new ZjAdmFactoryOrderByField();
        BeanUtil.copyProperties(fieldDto, field);
        field.setDisabled(fieldDto.getStatus());
        if (null != field.getId()) {
            this.updateById(field);
            return;
        }
        field.setId(IdWorker.getId());
        this.save(field);
    }

    // 获取排序字段列表
    @Override
    public Map<String, Object> getList(PageUtil pageUtil) {
        PageUtil.setPageLimit(pageUtil);
        QueryWrapper<ZjAdmFactoryOrderByField> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_update")
                .select("id", "type", "order_by_field", "order_by", "is_disabled AS disabled", "gmt_create", "gmt_update");
        Page<ZjAdmFactoryOrderByField> page = this.page(new Page<>(pageUtil.getPage(), pageUtil.getLimit()), wrapper);
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords().stream().map(field -> {
            FactoryOrderByFieldListVo vo = new FactoryOrderByFieldListVo();
            BeanUtil.copyProperties(field, vo);
            vo.setStatus(field.getDisabled());
            vo.setCreateTime(field.getGmtCreate());
            vo.setUpdateTime(field.getGmtUpdate());
            return vo;
        }).toList());
    }

    // 根据排序字段ID获取详情
    @Override
    public Object getDetail(Long id) {
        ZjAdmFactoryOrderByField orderByField = this.getById(id);
        Assert.notNull(orderByField, "排序字段数据不存在");
        FactoryOrderByFieldDto fieldDto = new FactoryOrderByFieldDto();
        BeanUtil.copyProperties(orderByField, fieldDto);
        fieldDto.setStatus(orderByField.getDisabled());
        return fieldDto;
    }

    // 批量启用或禁用排序字段
    @Override
    public void updateDisabledBatchById(List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        admFactoryOrderByFieldMapper.updateDisabledBatchById(ids, status);
    }
}
